package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    /**
     * 商品分页查询
     * @param brandId
     * @param categoryId
     * @param id
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        PageDTO<SpuDTO> pageDTOList=spuService.queryByPage(brandId,categoryId,id,page,rows,saleable);
        return ResponseEntity.ok(pageDTOList);
    }

    /**
     * 商品新增
     * @param spuDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 下架商品
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable
    ){
        spuService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改商品
     * @param spuDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 商品回显时，需要先根据id查询到商品的spu、sku、spuDetail等信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
       SpuDTO spuDTO= spuService.queryGoodsById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据商品id查询spuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id") Long id){
        SpuDetail spuDetail = detailService.getById(id);
        SpuDetailDTO spuDetailDTO=new SpuDetailDTO(spuDetail);
        return ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据商品id查询sku
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuById(@RequestParam("id") Long id){
        List<Sku> skuList = skuService.query().eq("spu_id", id).list();
        List<SkuDTO> skuDTOList = SkuDTO.convertEntityList(skuList);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 根据id集合查询SKU集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        List<Sku> skuList = skuService.listByIds(ids);
        List<SkuDTO> skuDTOList = SkuDTO.convertEntityList(skuList);
        return ResponseEntity.ok(skuDTOList);
    }


    /**
     * 根据spuId查询spu的所有规格参数键值对
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
      List<SpecParamDTO> paramDTOList= detailService.querySpecParams(id,searching);
        return ResponseEntity.ok(paramDTOList);
    }


    /**
     * 根据Id查询商品Spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpu(@PathVariable("id") Long id){
        Spu spu = spuService.getById(id);
        SpuDTO spuDTO=new SpuDTO(spu);
        return ResponseEntity.ok(spuDTO);
    }

}
